package com.jetpackframework.skin;

import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class SkinAttributeSets {
    public static String[] attrNames = {
        "background", "src","textColor","drawableLeft","drawableTop","drawableRight","drawableBottom"
    };
    private List<SkinView> skinViews = new ArrayList<>();
    public void look(View view, AttributeSet attrs) {
        List<SkinPair> skinPairs = new ArrayList<>();
        for (int i = 0 ; i < attrs.getAttributeCount() ; i++){
            String attributeName = attrs.getAttributeName(i);
            String attributeValue  = attrs.getAttributeValue(i);
            //针对color写死的处理
            if (attributeValue.startsWith("#")) {
                continue;
            }
            int resId = 0;
            //如果以？开头使用的则是属性值 或者定义在了value目录中
            if (attributeValue.startsWith("?")){
                int attrId = Integer.parseInt(attributeValue.substring(1));
                resId = SkinThemeUtils.getResId(view.getContext(), attrId);
            }else if (attributeValue.startsWith("@")) {  //一般以@开头
                resId = Integer.parseInt(attributeValue.substring(1));
            }
            if (resId != 0){
                SkinPair skinPair = new SkinPair(attributeName,resId);
                skinPairs.add(skinPair);
            }
        }
        SkinView skinView = new SkinView(view,skinPairs);
        skinView.apply();
        skinViews.add(skinView);
    }

    public void skinApply() {
        for (SkinView skinView : skinViews){
            skinView.apply();
        }
    }
}
